<?php
	class ModCp_Model_Profile extends Zend_Db_Table_Abstract
	{
		protected $_name = 'user';
		protected $_primary = 'user_id';
		protected $_db;
		
		public function __construct()
		{
			$this->_db = Zend_Registry::get('db');
		}
        
        public function getUserById($user_id)
        {
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->where('user_id = ?', $user_id);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function editUser($user_id, $email, $password = '', $avatar = '', $fullname, $gender, $birthday)
        {
            $where = 'user_id = ' . $user_id;
            if($avatar != '')
            {
                if($password != '')
                {
                    $data = array(
        				'email' => $email,
                        'password' => md5($password),
                        'avatar' => $avatar,
                        'fullname' => $fullname,
                        'gender' => $gender,
                        'birthday' => $birthday,
        			);
                }
                else
                {
                    $data = array(
        				'email' => $email,
                        'avatar' => $avatar,
                        'fullname' => $fullname,
                        'gender' => $gender,
                        'birthday' => $birthday,
        			);
                }
    			$result = $this->_db->update($this->_name, $data, $where);
    			if($result)
    			{
    				return true;
    			}
    			else
    			{
    				return false;
    			}
            }
            else
            {
                if($password != '')
                {
                    $data = array(
        				'email' => $email,
                        'password' => md5($password),
                        'fullname' => $fullname,
                        'gender' => $gender,
                        'birthday' => $birthday,
        			);
                }
                else
                {
                    $data = array(
        				'email' => $email,
                        'fullname' => $fullname,
                        'gender' => $gender,
                        'birthday' => $birthday,
        			);
                }
    			$result = $this->_db->update($this->_name, $data, $where);
    			if($result)
    			{
    				return true;
    			}
    			else
    			{
    				return false;
    			}
            }
        }
        
        public function delImg($user_id)
        {
            $data = array(
                'avatar' => 0,
            );
            $where = 'user_id = ' . $user_id;
            $result = $this->_db->update($this->_name, $data, $where);
            if($result)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
		
		public function checkPassword($user_id, $password)
		{
			$where = $this->_primary . " = " . $user_id . " AND password = '" . md5($password) . "'";
			$sql = $this->_db->select()
							 ->from($this->_name)
							 ->where($where);
			$result = $this->_db->fetchAll($sql);
			if(count($result) > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		public function changePasswordUser($user_id, $newPassword)
		{
			$data = array(
					'password' => md5($newPassword),
			);
			$where = $this->_primary . ' = ' . $user_id;
			$result = $this->_db->update($this->_name, $data, $where);
			if($result)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}